package com.example.tests;

import static com.example.tests.ContactDataGenerator.loadContactsFromCsvFile;
import static com.example.tests.ContactDataGenerator.loadContactsFromXmlFile;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import com.example.utils.SortedListOf;

public class ContactCreationTests extends TestBase {
	

	@DataProvider
	public Iterator<Object[]> contactsFromFile() throws IOException{
		return wrapContactsForDataProvider(loadContactsFromXmlFile(new File("contacts.xml"))).iterator();
	}

	@Test(dataProvider = "contactsFromFile")
	public void testContactCreationWithValidData(ContactData contact) throws Exception {		
		//save old contacts list
		SortedListOf<ContactData> oldContacts = app.getContactHelper().getContacts();
				
		//add new contact
		app.getContactHelper().createContact(contact);
		
		//save new contacts list
		SortedListOf<ContactData> newContacts = app.getContactHelper().getContacts();
		
		//compare lists
		if (contact.getEmail() == "")
			contact.withEmail(contact.getAdditionalEmail());
		if (contact.getHomePhone() == "")
			if (contact.getMobilePhone() == "")
				contact.withHomePhone(contact.getWorkPhone());
			else
				contact.withHomePhone(contact.getMobilePhone());
		
		assertThat(newContacts, equalTo(oldContacts.withAdded(contact)));
	}

}
